package com.efast.cafe.portal.service.api.impl;

import java.util.Date;
import java.util.UUID;

import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.cafe.framework.util.VersionUtil;
import com.efast.cafe.portal.bean.api.ApiModuleDefineBean;
import com.efast.cafe.portal.bean.api.ApiRequestUriBean;
import com.efast.cafe.portal.bean.api.ApiRequestUriQueryBean;
import com.efast.cafe.portal.dao.api.ApiInputParamDao;
import com.efast.cafe.portal.dao.api.ApiModuleDefineDao;
import com.efast.cafe.portal.dao.api.ApiModuleRequriDefDao;
import com.efast.cafe.portal.dao.api.ApiOutputParamDao;
import com.efast.cafe.portal.dao.api.ApiReqUriSampleDao;
import com.efast.cafe.portal.dao.api.ApiRequestUriDao;
import com.efast.cafe.portal.dao.api.ApiRequriObjDefDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.efast.cafe.portal.service.api.IApiRequestUriService;
import com.github.pagehelper.PageHelper;

/**
 * <p>
 * Service接口实现类
 * </p>
 * <p>
 * Table: api_request_uri - 请求uri路径
 * </p>
 *
 * @since 2017-01-16 06:21:38
 */
@Service
@Transactional
public class ApiRequestUriServiceImpl implements IApiRequestUriService {
	@Autowired
	private ApiRequestUriDao apiRequestUriDao;
	@Autowired
	private ApiModuleRequriDefDao apiModuleRequriDefDao;
	@Autowired
	private ApiInputParamDao apiInputParamDao;
	@Autowired
	private ApiOutputParamDao apiOutputParamDao;
	@Autowired
	private ApiRequriObjDefDao apiRequriObjDefDao;
	@Autowired
	private ApiReqUriSampleDao apiReqUriSampleDao;
	@Autowired
	private ApiModuleDefineServiceImpl apiModuleDefineServiceImpl;
	@Autowired
	private ApiModuleDefineDao apiModuleDefineDao;

	/**
	 * 删除uri信息
	 */
	@Override
	public void deleteUri(Integer uriId) {

		apiRequestUriDao.deleteUri(uriId);
	}

	/**
	 * 编辑uri
	 */
	@Override
	public void editUri(ApiRequestUriBean apiRequestUri) {
		// 此方法有bug 待修改
		Integer isExist = apiRequestUriDao.checkUriIsExist(apiRequestUri);
		if (isExist > 0) {
			throw new ServiceException("msg.portal.api.uri.path.exist");
		}
		ApiRequestUriBean oldApiRequestUri = apiRequestUriDao.getUriDetail(apiRequestUri.getSystemId(),
				apiRequestUri.getModuleId(), apiRequestUri.getId());
		if (oldApiRequestUri == null) {
			throw new ServiceException("msg.portal.api.updated");
		}
		if ( oldApiRequestUri.getReqUriStatus() == 1) {
			apiRequestUri.setReqUriVersion(oldApiRequestUri.getReqUriVersion());
			// 原uri已发布，修改后导致uri升版
			upGradeRequestUri(apiRequestUri);
		}else{
		apiRequestUri.setLastUpdateTime(new Date());
		apiRequestUri.setLastUpdateUser(SpringWebUtils.getRequestUser());
		apiRequestUriDao.editUri(apiRequestUri);
		// 删除以前参数
		apiOutputParamDao.delByUri(apiRequestUri.getId());
		apiInputParamDao.delByUri(apiRequestUri.getId());
		apiRequriObjDefDao.delByUriId(apiRequestUri.getId());
		apiReqUriSampleDao.delByUriId(apiRequestUri.getId());
		insertParam(apiRequestUri);
		}
	}

	/**
	 * 获取uri信息
	 */
	@Override
	public ApiRequestUriBean getUriDetail(Integer systemId, Integer moduleId, Integer uriId) {
		return apiRequestUriDao.getUriDetail(systemId, moduleId, uriId);
	}

	/**
	 * 插入uri 关联的输入输出参数
	 * 
	 * @param apiRequestUri
	 */
	private void insertParam(ApiRequestUriBean apiRequestUri) {
		/**
		 * 批量插入参数
		 */
		if (!apiRequestUri.getListResponseParam().isEmpty()) {
			apiOutputParamDao.insertByList(apiRequestUri);
		}
		if (!apiRequestUri.getListRequestParam().isEmpty()) {
			apiInputParamDao.insertByList(apiRequestUri);
		}
		/**
		 * 添加uri 和对象之间的关系
		 */
		if (!apiRequestUri.getObjIds().isEmpty()) {
			apiRequriObjDefDao.insertByIds(apiRequestUri);
		}
		/**
		 * 添加数据示例
		 */
		if (!apiRequestUri.getListUriSample().isEmpty()) {
			apiReqUriSampleDao.insertByUri(apiRequestUri);
		}
	}

	/**
	 * 查询uri当前分页信息
	 */
	@Override
	public ApiRequestUriQueryBean queryPageList(ApiRequestUriQueryBean queryBean) {
		if (queryBean.getObjName() != null && !"".equals(queryBean.getObjName())) {
			queryBean.setListUriObjDef(apiRequriObjDefDao.queryByObjName(queryBean.getObjName()));
		}
		PageHelper.startPage(queryBean.getStart(), queryBean.getLength());
		queryBean.setResultList(apiRequestUriDao.queryPageList(queryBean));
		queryBean.setTotalRows(apiRequestUriDao.queryPageCount(queryBean));
		return queryBean;
	}

	/**
	 * 新增uri
	 */
	@Override
	public Integer saveUri(ApiRequestUriBean apiRequestUri) {

		Integer isExist = apiRequestUriDao.checkUriIsExist(apiRequestUri);
		if (isExist > 0) {
			throw new ServiceException("msg.portal.api.uri.path.exist");
		}
		if (apiRequestUri.getReqUriVersion() == null || "".equals(apiRequestUri.getReqUriVersion())) {
			apiRequestUri.setReqUriVersion("0.0.1");
		}
		apiRequestUri.setReqUriStatus((byte) 3);
		apiRequestUri.setIsDel((byte) 0);
		apiRequestUri.setCreateTime(new Date());
		apiRequestUri.setCreateUser(SpringWebUtils.getRequestUser());
		apiRequestUri.setLastUpdateTime(new Date());
		apiRequestUri.setLastUpdateUser(SpringWebUtils.getRequestUser());
		if (apiRequestUri.getObjId() == null) {
			apiRequestUri.setObjId(UUID.randomUUID().toString());
		}
		// 新增uri
		apiRequestUriDao.saveUri(apiRequestUri);
		// 新增uri与module关联
		apiModuleRequriDefDao.saveUri(apiRequestUri);
		insertParam(apiRequestUri);
		return apiRequestUri.getId();
	}

	/**
	 * 通过url查询uri详细信息
	 */
	@Override
	public ApiRequestUriBean searchByUrl(ApiRequestUriBean apiRequestUriBean) {
		return apiRequestUriDao.searchByUrl(apiRequestUriBean.getSystemPath(), apiRequestUriBean.getModuleUri(),
				apiRequestUriBean.getPathUri(), apiRequestUriBean.getSystemVersion());
	}

	/**
	 * ur升版
	 * 
	 * @param apiRequestUri
	 */
	private void upGradeRequestUri(ApiRequestUriBean apiRequestUri) {
		// 判断uri的升版是否引起模块升版
		Integer moduleId = apiRequestUri.getModuleId();
		if (apiRequestUri.getModuleStatus() == 1) {
			ApiModuleDefineBean apiModuleDefineBean = apiModuleDefineDao
					.queryApiModuleDefineById(apiRequestUri.getSystemId(), apiRequestUri.getModuleId());
			apiModuleDefineBean.setModuleStatus((byte) 3);
			// 模块已发布，引起模块升版
			moduleId = apiModuleDefineServiceImpl.moduleAndSystemUpGrade(apiModuleDefineBean, true);
		}
		// 升版uri并修改模块与uri的关系
		apiRequestUri.setParentId(apiRequestUri.getId());
		apiRequestUri.setReqUriVersion(VersionUtil.newVersion(apiRequestUri.getReqUriVersion()));
		apiRequestUri.setModuleId(moduleId);
		apiModuleRequriDefDao.deleteModuleRequestUriDef(apiRequestUri.getModuleId(), apiRequestUri.getId());
		// 新增uri并更新uri与模块的关系
		saveUri(apiRequestUri);
	}

	/**
	 * 通过完整url查询请求路径
	 */
	@Override
	public ApiRequestUriQueryBean urlPageList(ApiRequestUriQueryBean queryBean) {
		PageHelper.startPage(queryBean.getStart(), queryBean.getLength());
		queryBean.setResultList(apiRequestUriDao.urlPageList(queryBean));
		queryBean.setTotalRows(apiRequestUriDao.urlPageCount(queryBean));
		return queryBean;
	}

	/**
	 * uri复用
	 */
	@Override
	public void reuseApiUridefine(ApiRequestUriBean apiRequestUriBean) {
		apiModuleRequriDefDao.saveUri(apiRequestUriBean);
	}

}